#!/usr/bin/env python
#coding=utf-8
# author:wade
# contact: 317909531@qq.com
# datetime:2020/4/28 21:11

"""
文件说明：
"""
# -*- coding: utf-8 -*-
from __future__ import print_function

import ssl, hmac, base64, hashlib
from datetime import datetime as pydatetime

import requests

from shareAPI.views import APIResponse


def getBookInfo(isbn):
    # 云市场分配的密钥Id
    secretId = "AKID3a4mzic7vfi8B4s2tuf2kpPv6fzbh0iti9to"
    # 云市场分配的密钥Key
    secretKey = "8fqj8DqZdh4UAeknOgWU7gUn0rLh2rKdyr37Y0y8"
    source = "market"
    # 签名
    datetime = pydatetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
    signStr = "x-date: %s\nx-source: %s" % (datetime, source)
    sign = base64.b64encode(hmac.new(secretKey.encode('utf-8'), signStr.encode('utf-8'), hashlib.sha1).digest())
    auth = 'hmac id="%s", algorithm="hmac-sha1", headers="x-date x-source", signature="%s"' % (secretId, sign.decode('utf-8'))

    # 请求方法
    method = 'GET'
    # 请求头
    headers = {
        'X-Source': source,
        'X-Date': datetime,
        'Authorization': auth,
    }
    # 查询参数
    queryParams = {
        'isbn': isbn}
    # body参数（POST方法下存在）
    bodyParams = {
    }
    # url参数拼接
    url = 'https://service-7owdfa8z-1255468759.sh.apigw.tencentcs.com/release/isbnpro'


    response = requests.get(url,data=queryParams,headers=headers)
    print(response.json())
    response_Json = response.json()
    print(type(response_Json))
    for key in response_Json:
        print(key)
    if response_Json['showapi_res_body']['ret_code']==-1:
        return APIResponse(0,msg='数据库中找不到这本书，请手动录入')
    else:
        return response_Json['showapi_res_body']['datas'][0]
